public class UserStoryStatusIdeaURLControl 
{
	//this method updates the user story status and idea url on the case every time the user story is updated. This is done
	//to trigger the appropriate customer communication.
	public static void UserStoryStatusIdeaURLToCase(User_Story__c[] UserStories)
	{
		List<id> userStoryIdList = new List<id>();
		for (integer x = 0; x < UserStories.size(); x++)
		{
			userStoryIdList.add(UserStories[x].id);
		}
		Map<id,solution> solutionMap = new Map<id,Solution>();
		For(solution s:[select id,user_story__c,user_story__r.status__c,user_story__r.idea_url__c from solution where user_story__c in :userStoryIdList])
		{
			solutionMap.put(s.id,s);
		}
		List<case> updateCases = new List<case>();
		for(case c:[select user_story_status__c,idea_url__c,solution__c from case where solution__c in :solutionMap.keyset()])
		{
			solution caseSol = solutionMap.get(c.solution__c);
			c.user_story_status__c = caseSol.user_story__r.status__c;
			c.idea_url__c = caseSol.user_story__r.idea_url__c;
			updateCases.add(c);
		}
		update updateCases;
		
	}
	
	//If an Idea or its User story link is deleted, this method removes the Idea URL from the User Story, as well as the status if the OnTime ID is null
	public static void DeleteUpdateIdea(Idea[] ideas,Idea[] ideasOld)
	{
		List<id> userStoryIdList = new List<id>();
		for (integer x = 0; x < ideasOld.size(); x++)
		{
			If(trigger.isBefore)If(trigger.isUpdate)
			{
				If(ideas[x].user_story__c != ideasOld[x].user_story__c)If(ideas[x].user_story__c == Null)
				{
					userStoryIdList.add(ideasOld[x].user_story__c);
				}
			}
		 	If(trigger.isBefore)If(trigger.isDelete)
		 	{
		 		userStoryIdList.add(ideasOld[x].user_story__c);
		 	}
		}
		If (userStoryIdList.size()>0)
		{
			List<user_story__c> updateUserStories = New List<user_story__c>();
			for(user_story__c u : [select status__c,idea_url__c,OnTime_Id__c from User_story__c where id in :userStoryIdList])
			{
				If(u.OnTime_Id__c == null)
				{
					u.Status__c = null;
				}
				u.Idea_url__c = null;
				updateUserStories.add(u);
			}
			update updateUserStories;
		}		
	}
	
	//Clears the user story status and idea link on all cases if a user story object is deleted. Called by a before delete trigger.
	public static void DeleteUserStory(User_Story__c[] userStoriesOld)
	{
		List<id> userStoryIdList = new List<id>();
		for (integer x = 0; x < userStoriesOld.size(); x++)
		{
			userStoryIdList.add(userStoriesOld[x].id);
		}
		If (userStoryIdList.size()>0)
		{
			List<Id> solutionIdList = New List<id>();
			For(solution s:[select id from solution where user_story__c in :userStoryIdList])
			{
				solutionIdList.add(s.id);
			}
			List<case> updateCases = new List<case>();
			for(case c:[select user_story_status__c,idea_url__c from case where solution__c in :solutionIdList])
			{
				c.user_story_status__c = Null;
				c.idea_url__c = Null;
				updateCases.add(c);
			}
			update updateCases;
		}
	}
	
	//clears the user story status and idea url on all cases associated to a deleted solution or a solution who's user story link has been broken. 
	//called by a before update,before delete trigger.
	public static void DeleteUpdateSolution(Solution[] Solutions,Solution[] SolutionsOld)
	{
		List<id> solutionIdList = new List<id>();
		for (integer x = 0; x < SolutionsOld.size(); x++)
		{
			If(trigger.isBefore)If(trigger.isUpdate)
			{
				If(Solutions[x].user_story__c != SolutionsOld[x].user_story__c)If(Solutions[x].user_story__c == Null)
				{
					solutionIdList.add(SolutionsOld[x].Id);
				}
			}
		 	If(trigger.isBefore)If(trigger.isDelete)
		 	{
		 		solutionIdList.add(SolutionsOld[x].Id);
		 	}
		}
		If (solutionIdList.size()>0)
		{
			List<case> updateCases = New List<case>();
			for(case c : [select user_story_status__c,idea_url__c from case where solution__c in :solutionIdList])
			{
				c.user_story_status__c = null;
				c.idea_url__c = null;
				updatecases.add(c);
			}
			update updateCases;
		}		
	}
	
	//clears user story status and idea url on a case when the solution link is broken. called by an after update trigger.
	public static void UpdateCase(Case[] Cases)
	{
		List<id> caseIdList = new List<id>();
		for (integer x = 0; x < Cases.size(); x++)
		{
			If(((Cases[x].user_story_status__c != null)||(Cases[x].idea_url__c != null))&(Cases[x].solution__c == Null))
			{
				caseIdList.add(Cases[x].Id);
			}
		}
		If (caseIdList.size()>0)
		{
			List<case> updateCases = New List<case>();
			for(case c : [select user_story_status__c,idea_url__c from case where Id in :caseIdList])
			{
				c.user_story_status__c = null;
				c.idea_url__c = null;
				updatecases.add(c);
			}
			update updateCases;
		}	
	}	
}